import numpy as np 
from matplotlib import pyplot as plt 

def evaluate_Kc_hard_spring_wheat(x):
    return -0.01925 + 0.002641*x + (1.05e-07)*(x**2) - (2.23e-09)*(x**3) + (3.57e-13)*(x**4)

def evaluate_Kc_soft_spring_wheat(x):
    return -0.0207 + 0.00266*x + (4.7e-08)*(x**2) - (2.0e-09)*(x**3) + (2.7e-13)*(x**4)


length_of_season = 85
def generate_Kc(i):
    samp_temps = np.loadtxt('avg_temp.txt')
    #Randomly generate average temperatures

    _mean = np.mean(samp_temps)
    _stddev =  np.std(samp_temps)
    np.random.seed(i)
    avg_temp = np.random.normal(_mean, _stddev, length_of_season)

    for i in range(len(avg_temp)):
        if avg_temp[i] > 25.0:
            avg_temp[i] = 25.0
            pass 
        pass

    base_temp = 5 
    gdd = avg_temp - base_temp
    gdd_cum = np.cumsum(gdd)
    kc_calculated = evaluate_Kc_soft_spring_wheat(gdd_cum)
    plt.plot(kc_calculated)
    return kc_calculated
    

#Create an array of crop coefficient values for the data generation
num_of_season = 900

crop_coeoff = np.zeros(int(num_of_season*length_of_season))

for i in range(num_of_season):
    crop_coeoff[int(i*length_of_season):int((i+1)*length_of_season)] = generate_Kc(i)
    pass

#np.savetxt("crop_coeff_n.txt", crop_coeoff)